package project.taobao.commons.activemq.utils;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import project.taobao.commons.activemq.factory.ConsumerFactory;
import project.taobao.commons.activemq.factory.ProducerFactory;

import javax.jms.Destination;
import javax.jms.Queue;

public class ActiveMqUtils  {
    private static JmsTemplate jmsTemplate = null;
    private static ApplicationContext context = null;

    static {
        try{
            context = new ClassPathXmlApplicationContext("application1.xml");
        } catch (Exception e){

        }

    }

    // methods
    public static JmsTemplate getJmsTemplate(){
        if(jmsTemplate == null){
            jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate");
        }
         return jmsTemplate;
    }

    public static Destination getDestination(String queueName){
        return (Queue)context.getBean(queueName);
    }

    public static ConsumerFactory getConsumerFactory(){
        return (ConsumerFactory)context.getBean(ConsumerFactory.class);
    }

    public static ProducerFactory getProducerFactory(){
        return (ProducerFactory)context.getBean(ProducerFactory.class);
    }

    @Deprecated
    public static ActiveMQConnectionFactory getAcMQConnectionFactory(){
        return (ActiveMQConnectionFactory)context.getBean("connectionFactory");
    }

    public static SingleConnectionFactory getConnectionFactory(){
        return (SingleConnectionFactory)context.getBean("connectionFactory");
    }

    
    // getters, setters

    // constructors
}
